package com.livestocksolutions;

import java.util.HashMap;

import com.livestocksolutions.util.UpdateHistory;

/* -- Version History --
 *  1.0 : ???          - Initial version.
 *  
 *  1.1 : 14 September 2012 - 
 */

/**
 * Stores all Identification for a specific Bovine.
 * @author Matt Lang
 * @author Adam Parr
 * @version 1.1
 * 
 * @.modified 22 September 2012
 */
public class Eartag {
	/**
	 * Keep track of any changes, until this Eartag is assigned to an owner.
	 */
	protected HashMap<String,UpdateHistory> update = new HashMap<String,UpdateHistory>();
	/**
	 * Store the Owners ID.
	 */
	protected long owner;
	/**
	 * Visual Eartag id.
	 */
	private String eartagID ="";
	/**
	 * RFID of the NLIS tag.
	 */
	private String rfID ="";
	/**
	 * NLIS id.
	 */
	private String nlisID ="";
	
	/**
	 * Get the value of the Eartag id.
	 * @return Eartag id associated with this animal.
	 */
	public String getEartagID(){
		return eartagID==null?"":eartagID;
	}
	
	/**
	 * Get the value of the RFID of the NLIS tag.
	 * @return RFID associated with this animal.
	 */
	public String getRfID(){
		return rfID==null?"":rfID;
	}
	
	/**
	 * Get the value of the NLIS id.
	 * @return NLIS id associated with this animal.
	 */
	public String getNlisID(){
		return nlisID==null?"":nlisID;
	}
	
	/**
	 * Set the Visual eartag id.
	 * @param eartagID The new String that will visually identify the animal.
	 */
	public void setEartagID(String eartagID){
		if(this.eartagID==null || !this.eartagID.equals(eartagID)){
			update.put("EARTAGID", new UpdateHistory(eartagID));
			this.eartagID = eartagID;
		}
	}
	
	/**
	 * Set the RFID of the NLIS tag.
	 * @param rfID The new String that will become the NLIS RFID value.
	 */
	public void setRfID(String rfID){
		if(this.rfID==null || !this.rfID.equals(rfID)){
			update.put("RFID", new UpdateHistory(rfID));
			this.rfID = rfID;
		}
	}
	
	/**
	 * Set the NLIS id of the tag.
	 * @param nlisID The new String that will become the NLIS value.
	 */
	public void setNlisID(String nlisID){
		if(this.nlisID==null || !this.nlisID.equals(nlisID)){
			update.put("NLIS", new UpdateHistory(nlisID));
			this.nlisID = nlisID;
		}
	}
	
	/**
	 * Set the Owner of this Eartag to the given Owner
	 * @param owner The Bovine that the Eartag belongs to.
	 */
	public void setOwner(Bovine owner){
		this.owner = owner.getID();
		update.put("OWNER", new UpdateHistory(String.valueOf(this.owner)));
	}
	
	/**
	 * Updates this Object with the updated values from the different version.
	 * @param changes Eartag that represents this Object in a different version.
	 * @return <tt>true</tt> if changes is not null and changes does not equal this object, <tt>false</tt> otherwise.
	 */
	public boolean update(Eartag changes){
		if(changes!=null && this!=changes){
			if(updateBefore("OWNER",changes)){
				owner=Long.parseLong(changes.update.get("OWNER").getValue());
				update.put("OWNER", new UpdateHistory(String.valueOf(owner)));
			}
			
			if(updateBefore("NLIS",changes)){
				nlisID=changes.update.get("NLIS").getValue();
				update.put("NLIS", new UpdateHistory(nlisID));
			}
			
			if(updateBefore("RFID",changes)){
				rfID=changes.update.get("RFID").getValue();
				update.put("RFID", new UpdateHistory(rfID));
			}
			
			if(updateBefore("EARTAGID",changes)){
				eartagID=changes.update.get("EARTAGID").getValue();
				update.put("EARTAGID", new UpdateHistory(eartagID));
			}
			return true;
		}
		return false;
	}
	
	/**
	 * Checks to see if the modification date of the item at this position is before the changed value
	 * @param key The String key at which to compare the modification dates of the modified objects.
	 * @param changes The Eartag that represents this Eartag on another system.
	 * @return <tt>true</tt> if the old value was modified before the changed value. <tt>false</tt> otherwise.
	 */
	protected boolean updateBefore(String key,Eartag changes){
		if(this.update.get(key)==null && changes.update.get(key)!=null){
			return true;
		}else if(changes.update.get(key)==null){
			return false;
		}
		return !this.update.get(key).getDate().after(changes.update.get(key).getDate());
	}
}
